﻿using Jacobi.Seesmic.FeedReader.FeedItemView;
using Jacobi.Seesmic.FeedReader.Model;
using Jacobi.Seesmic.FeedReader.Utilities;
using Seesmic.Sdp.Extensibility;

namespace Jacobi.Seesmic.FeedReader
{
    /// <summary>
    /// Creates <see cref="TimelineItemContainer"/> instances for <see cref="TimelineFeedItem"/>s
    /// published by a <see cref="FeedSource"/>.
    /// </summary>
    internal sealed class TimelineItemBuilder
    {
        private FeedManager _feedManager;

        /// <summary>
        /// Constructs a new instance for the specified <paramref name="feedManager"/>.
        /// </summary>
        /// <param name="feedManager">Must not be null.</param>
        public TimelineItemBuilder(FeedManager feedManager)
        {
            feedManager.ThrowIfArgumentIsNull("feedManager");

            _feedManager = feedManager;
        }

        /// <summary>
        /// Creates a <see cref="TimelineItemContainer"/> instance for
        /// the specified <paramref name="feedItem"/>.
        /// </summary>
        /// <param name="feedItem">Must not be null.</param>
        /// <returns>Never returns null.</returns>
        public TimelineItemContainer CreateContainer(TimelineFeedItem feedItem)
        {
            var vm = CreateViewModel(feedItem);

            return CreateContainer(vm);
        }

        /// <summary>
        /// Creates a <see cref="FeedItemViewModel"/> around the specified <paramref name="feedItem"/>.
        /// </summary>
        /// <param name="feedItem">Must not be null.</param>
        /// <returns>Never returns null.</returns>
        public FeedItemViewModel CreateViewModel(TimelineFeedItem feedItem)
        {
            feedItem.ThrowIfArgumentIsNull("feedItem");

            var vm = new FeedItemViewModel(feedItem);

            vm.Tag = _feedManager;

            return vm;
        }

        /// <summary>
        /// Creates a <see cref="TimelineItemContainer"/> around the specified <paramref name="viewModel"/>.
        /// </summary>
        /// <param name="viewModel">Must not be null.</param>
        /// <returns>Never return null.</returns>
        /// <remarks>The returned instance is marked as 'Unread'.</remarks>
        public static TimelineItemContainer CreateContainer(FeedItemViewModel viewModel)
        {
            var containerItem = new TimelineItemContainer(viewModel);

            containerItem.IsUnread = true;

            return containerItem;
        }
    }
}